package ua.kpi.ipsa.expert.api.client;

import java.awt.event.*;
import java.net.*;

import javax.swing.*;

import ua.kpi.ipsa.expert.api.net.*;

import net.miginfocom.swing.*;

/**
 * 
 * @author doctorpro
 * 
 */
public class LoginForm extends JPanel {
	private JTextField txtAdmin;
	private JPasswordField passwordField;
	private ImageIcon greenCircle;
	private ImageIcon yellowCircle;
	private ImageIcon redCircle;
	private JLabel picLabel;
	private JButton btnOk;
	private JLabel srvLabel;

	private ConnectionApplicationListener applicationListener;

	/**
	 * Create the panel.
	 */
	public LoginForm(ConnectionApplicationListener applicationListener) {
		this.applicationListener = applicationListener;
		setLayout(new MigLayout("", "[][grow]", "[][][][grow]"));

		greenCircle = createImageIcon("circle_green.png");
		redCircle = createImageIcon("circle_red.png");
		yellowCircle = createImageIcon("circle_yellow.png");

		JLabel loginLabel = new JLabel("Login:");
		add(loginLabel, "cell 0 0,alignx trailing");

		txtAdmin = new JTextField();
		txtAdmin.setText("admin");
		add(txtAdmin, "cell 1 0,alignx left,growy");
		txtAdmin.setColumns(17);

		JLabel passwordPanel = new JLabel("Password:");
		add(passwordPanel, "cell 0 1,alignx trailing");

		passwordField = new JPasswordField();
		passwordField.setText("nimda");
		passwordField.setColumns(17);
		add(passwordField, "cell 1 1,alignx left");

		picLabel = new JLabel("");
		picLabel.setIcon(yellowCircle);
		add(picLabel, "cell 0 2,alignx right");

		srvLabel = new JLabel("Unknown");
		add(srvLabel, "cell 1 2,alignx left");

		JPanel panel = new JPanel();
		add(panel, "cell 0 3 2 1,alignx center,aligny top");
		panel.setLayout(new MigLayout("",
				"[78px:78px:78px,center][78px:78px:78px,center]", "[]"));

		btnOk = new JButton("Login");
		panel.add(btnOk, "cell 0 0,growx");
		btnOk.addActionListener(new OkHandler());

		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new CancelHandler());
		panel.add(btnCancel, "cell 1 0,growx");

		onServerUnknown();
	}

	/**
	 * 
	 */
	public void onServerUnknown() {
		btnOk.setEnabled(false);
		passwordField.setEnabled(false);
		txtAdmin.setEnabled(false);
		srvLabel.setText("Unknown");
		picLabel.setIcon(yellowCircle);
	}

	/**
	 * 
	 */
	public void onServerOffline() {
		btnOk.setEnabled(false);
		passwordField.setEnabled(false);
		txtAdmin.setEnabled(false);
		srvLabel.setText("Server offline");
		picLabel.setIcon(redCircle);
	}

	/**
	 * 
	 */
	public void onServerOnline() {
		btnOk.setEnabled(true);
		passwordField.setEnabled(true);
		txtAdmin.setEnabled(true);
		srvLabel.setText("Server online");
		picLabel.setIcon(greenCircle);
	}

	/**
	 * 
	 * @param path
	 * @return
	 */
	private ImageIcon createImageIcon(String path) {
		URL url = ua.kpi.ipsa.expert.api.adminpanel.AdminpanelGui.class
				.getResource(path);
		if (url == null) {
			return null;
		} else {
			return new ImageIcon(url);
		}
	}

	public JTextField getTextField() {
		return txtAdmin;
	}

	public JPasswordField getPasswordField() {
		return passwordField;
	}

	protected JLabel getPicLabel() {
		return picLabel;
	}

	/**
	 * 
	 * @author doctorpro
	 * 
	 */
	private class OkHandler implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {

			// try to login
			String login = txtAdmin.getText();
			String pass = new String(passwordField.getPassword());
			try {
				applicationListener.applyConnection(login, pass);
			} catch (ConsoleException e1) {
				JOptionPane.showMessageDialog(null,
						e1.getMessage());
				// e1.printStackTrace();
			}
		}
	}

	/**
	 * 
	 * @author doctorpro
	 * 
	 */
	private class CancelHandler implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	}

	protected JLabel getSrvLabel() {
		return srvLabel;
	}
}
